package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.AdminCategoryL1VO;
import com.cskaoyan.bean.vo.AdminCategoryVO2;
import com.cskaoyan.bean.vo.WxHomeIndexFloorGoodsVO;
import com.cskaoyan.bean.vo.WxHomeIndexVO;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * \* User: 余雨健
 * \* Date: 2022/10/14
 * \* Time: 14:59
 **/
@Service
public class WxHomeServiceImpl implements WxHomeService{
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Override
    public WxHomeIndexVO returnIndexOrderList() {
        Random random = new Random();
        int numberOfElements = 8;
        WxHomeIndexVO wxHomeIndexVO = new WxHomeIndexVO();
        MarketAdExample marketAdExample = new MarketAdExample();
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        wxHomeIndexVO.setBanner(marketAds);


        MarketBrandExample marketBrandExample = new MarketBrandExample();
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        if(marketBrands.size() > 8){
            List<MarketBrand> TrueMarketBrands = new ArrayList<>();
            for (int i = 0; i < numberOfElements; i++) {
                int randomIndex = random.nextInt(marketBrands.size());
                MarketBrand marketBrand = marketBrands.get(randomIndex);
                marketBrands.remove(randomIndex);
                TrueMarketBrands.add(marketBrand);
            }
            wxHomeIndexVO.setBrandList(TrueMarketBrands);
        }else {
            wxHomeIndexVO.setBrandList(marketBrands);
        }


        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria3 = marketCategoryExample.createCriteria();
        criteria3.andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        if(marketCategories.size() > 10){
            List<MarketCategory> TrueMarketCategory = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                TrueMarketCategory.add(marketCategories.get(i));
            }
            wxHomeIndexVO.setChannel(TrueMarketCategory);
        }else {
            wxHomeIndexVO.setChannel(marketCategories);
        }


        MarketCouponExample marketCouponExample = new MarketCouponExample();
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        if(marketCoupons.size() > 4){
            List<MarketCoupon> TrueMarketCoupons = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                int randomIndex = random.nextInt(marketCoupons.size());
                MarketCoupon marketCoupon = marketCoupons.get(randomIndex);
                marketBrands.remove(randomIndex);
                TrueMarketCoupons.add(marketCoupon);
            }
            wxHomeIndexVO.setCouponList(TrueMarketCoupons);
        }else {
            wxHomeIndexVO.setCouponList(marketCoupons);
        }

        List<AdminCategoryVO2> list1 = marketCategoryMapper.selectPicByprimaryKey(0);
        List<WxHomeIndexFloorGoodsVO> TrueGoodsList = new ArrayList<>();
        List<AdminCategoryVO2> List = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            int randomIndex = random.nextInt(list1.size());
            AdminCategoryVO2 adminCategoryVO2 = list1.get(randomIndex);
            list1.remove(randomIndex);
            List.add(adminCategoryVO2);
        }
        for (AdminCategoryVO2 adminCategoryVO2 : List) {
            WxHomeIndexFloorGoodsVO wxHomeIndexFloorGoodsVO = new WxHomeIndexFloorGoodsVO();
            wxHomeIndexFloorGoodsVO.setId(adminCategoryVO2.getId());
            wxHomeIndexFloorGoodsVO.setName(adminCategoryVO2.getName());
            List<MarketGoods> GoodsList = new ArrayList<>();
            MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = marketCategoryExample1.createCriteria();
            criteria1.andPidEqualTo(adminCategoryVO2.getId());
            List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
            for (MarketCategory marketCategory : marketCategories1) {
                if(GoodsList.size() < 6){
                    MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
                    MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
                    criteria.andCategoryIdEqualTo(marketCategory.getId());
                    List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
                    for (MarketGoods marketGood : marketGoods) {
                        if(marketGoods.size() > 0 && GoodsList.size() < 6){
                            GoodsList.add(marketGood);
                        }
                    }
                }else {
                    break;
                }
            }
            wxHomeIndexFloorGoodsVO.setGoodsList(GoodsList);
            TrueGoodsList.add(wxHomeIndexFloorGoodsVO);
        }
        wxHomeIndexVO.setFloorGoodsList(TrueGoodsList);



        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = marketGoodsExample1.createCriteria();
        criteria1.andIsHotEqualTo(true);
        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample1);
        if(marketGoods1.size() > 6){
            List<MarketGoods> TrueMarketGoods1 = new ArrayList<>();
            for (int i = 0; i < numberOfElements; i++) {
                int randomIndex = random.nextInt(marketGoods1.size());
                MarketGoods marketGoods = marketGoods1.get(randomIndex);
                marketGoods1.remove(randomIndex);
                TrueMarketGoods1.add(marketGoods);
            }
            wxHomeIndexVO.setHotGoodsList(TrueMarketGoods1);
        }else {
            wxHomeIndexVO.setHotGoodsList(marketGoods1);
        }

        MarketGoodsExample marketGoodsExample2 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria2 = marketGoodsExample2.createCriteria();
        criteria2.andIsNewEqualTo(true);
        List<MarketGoods> marketGoods2 = marketGoodsMapper.selectByExample(marketGoodsExample2);
        if(marketGoods2.size() > 6){
            List<MarketGoods> TrueMarketGoods2 = new ArrayList<>();
            for (int i = 0; i < numberOfElements; i++) {
                int randomIndex = random.nextInt(marketGoods2.size());
                MarketGoods marketGoods = marketGoods2.get(randomIndex);
                marketGoods2.remove(randomIndex);
                TrueMarketGoods2.add(marketGoods);
            }
            wxHomeIndexVO.setNewGoodsList(TrueMarketGoods2);
        }else {
            wxHomeIndexVO.setNewGoodsList(marketGoods2);
        }

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        wxHomeIndexVO.setTopicList(marketTopics);

        return wxHomeIndexVO;
    }
}
